<!-- 公共组件 -->
<template>
  <div class="home">
    <lb-header @handleAccount="handleAccount"></lb-header>
    <sidebar></sidebar>
    <div
      class="container"
      :style="{
        'margin-right': adSwitch ? '220px' : '0px',
        'margin-left': sideBarSwitch ? '280px' : '120px'
      }"
    >
      <!-- 内容区域 -->
      <div class="main">
        <transition name="fade" mode="out-in">
          <keep-alive :max="1">
            <router-view v-if="$route.meta.keepAlive"></router-view>
          </keep-alive>
        </transition>
        <transition name="fade" mode="out-in">
          <router-view v-if="!$route.meta.keepAlive"></router-view>
        </transition>
      </div>
      <div class="home-footer" v-html="fHtml"></div>
    </div>
    <!-- 右侧展开折叠栏 ad -->
    <!-- <ad></ad> -->
    <!-- <lb-footer></lb-footer> -->

    <!-- 编辑账户 -->
    <el-dialog title="编辑账户" :visible.sync="dialogVisible" width="520px">
      <el-form
        @submit.native.prevent
        :model="accountForm"
        :rules="accountFormRules"
        ref="accountForm"
        label-width="130px"
        class="basic-form"
        style="padding-right: 30px"
      >
        <el-form-item label="账号" prop="account">
          <el-input
            v-model="accountForm.account"
            :disabled="true"
            placeholder="账号"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item
          label="原密码"
          prop="old_passwd"
        >
          <el-input
            v-model="accountForm.old_passwd"
            placeholder="请输入原密码"
          ></el-input>
        </el-form-item> -->
        <el-form-item label="新密码" prop="new_passwd">
          <el-input
            v-model="accountForm.new_passwd"
            placeholder="请输入新密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="again_passwd">
          <el-input
            v-model="accountForm.again_passwd"
            placeholder="请确认新密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('accountForm')">{{
            $t('action.submit')
          }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import lbHeader from './header'
import lbFooter from './footer'
import sidebar from './sidebar'
import ad from './ad'
import { mapGetters, mapState, mapMutations } from 'vuex'
export default {
  name: 'Home',
  data () {
    let validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入密码'))
      } else if (!/^(\S){6,20}$/.test(value)) {
        callback(new Error('请输入6-20位非空白符的字符!'))
      } else {
        callback()
      }
    }
    let validateAgainPassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.accountForm.new_passwd) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    return {
      isChangeRoutes: true,
      fHtml: '',
      dialogVisible: false,
      accountForm: {
        account: window.sessionStorage.getItem('ms_username'),
        new_passwd: ''
      },
      accountFormRules: {
        // old_passwd: {
        //   required: true,
        //   type: 'string',
        //   validator: validatePassword,
        //   trigger: 'blur'
        // },
        new_passwd: {
          required: true,
          type: 'string',
          validator: validatePassword,
          trigger: 'blur'
        },
        again_passwd: {
          required: true,
          type: 'string',
          validator: validateAgainPassword,
          trigger: 'blur'
        }
      },
      authForm: {
        pass: ''
      },
      authFormRules: {
        pass: { required: true, type: 'string', message: '请输入获取到的激活口令', trigger: 'blur' }
      },
      modelAuthImg: 'https://lbqny.migugu.com/admin/card/WechatIMG322.jpeg',
      modelAuthList: {
        // https://lbqny.migugu.com/weixin/WechatIMG495.jpeg
        longbing_card: 'https://lbqny.migugu.com/admin/card/WechatIMG322.jpeg',
        longbing_radarstore: 'https://lbqny.migugu.com/admin/card/WechatIMG322.jpeg',
        longbing_decorate: 'https://lbqny.migugu.com/admin/card/WechatIMG322.jpeg',
        longbing_shortvideo: 'https://lbqny.migugu.com/admin/card/WechatIMG322.jpeg',
        longbing_restaurant: 'https://lbqny.migugu.com/admin/card/WechatIMG322.jpeg',
        longbing_member: 'https://lbqny.migugu.com/admin/card/WechatIMG322.jpeg'
      }
    }
  },
  components: {
    lbHeader,
    lbFooter,
    sidebar,
    ad
  },
  created () {
    this.getCopyrightInfo()
  },
  computed: {
    ...mapGetters(['adSwitch', 'sideBarSwitch']),
    ...mapState({
      routesItem: state => state.routes
    })
  },
  methods: {
    ...mapMutations(['changeIsShowPrompt', 'changeRoutesItem']),
    handleAccount (flag) {
      this.dialogVisible = flag
    },
    submitForm (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          let {
            new_passwd: pass
          } = JSON.parse(JSON.stringify(this.accountForm))
          this.$api.base.updatePasswd({
            pass
          }).then(res => {
            if (res.code === 200) {
              this.$message.success(this.$t('tips.successRev'))
              sessionStorage.removeItem('lbtk') // 删除token
              sessionStorage.removeItem('ms_username') // 删除用户名
              this.$router.push('/login')
            }
          })
        }
      })
    },
    submitAuthForm (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          let { pass } = this.authForm
          this.$api.baseGiveAuth({
            pass
          }).then(res => {
            if (res.code === 200) {
              this.$message.success(this.$t('tips.successSub'))
              this.changeRoutesItem({ key: 'isFreeAuth', val: false })
            }
          })
        }
      })
    },
    getCopyrightInfo () {
      let { systemCopyInfo } = this.routesItem
      if (systemCopyInfo === 1) {
        this.fHtml = ''
      } else {
        if (!systemCopyInfo.footerleft) {
          this.fHtml =
            `<div>Powered by <a class='el-link el-link--info' href="http://www.we7.cc"><b>微擎</b></a>
                  v${systemCopyInfo.version} © 2014-2015
                  <a class='el-link el-link--info' href="http://www.we7.cc">www.we7.cc</a></div>`
        } else {
          this.fHtml = systemCopyInfo.footerleft
        }
        if (systemCopyInfo.icp) {
          this.fHtml = this.fHtml +
            `<div>备案号：<a class='el-link el-link--info' href="http://www.miitbeian.gov.cn" target="_blank">${systemCopyInfo.icp}</a></div>`
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.home {
  border: 1px solid transparent;
  background: #f0f0f0;

  .container {
    margin: 70px 0 0 140px;
    padding: 20px;
    background: $bgThemeColor;
    transition: margin 0.2s linear;

    .main {
      width: 100%;
      background: #fff;
      position: relative;
      min-height: calc(100vh - 70px - 92px);
    }

    .home-footer {
      height: 49px;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #ccc;
      font-size: 12px;
      z-index: 10;
      margin: 0 auto;
      line-height: 1;
      width: 50%;
    }
  }
}

.fade-transform-leave-active,
.fade-transform-enter-active {
  transition: all 0.5s;
}

.fade-transform-enter {
  opacity: 0;
  transform: translateX(-30px);
}

.fade-transform-leave-to {
  opacity: 0;
  transform: translateX(30px);
}

.slide-fade-enter-active {
  transition: all 2s ease;
}

.slide-fade-leave-active {
  transition: all 0s ease;
}

.slide-fade-enter,
.slide-fade-leave-to {
  transform: translateX(20px);
  opacity: 0;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}

.fade-leave,
.fade-enter-to {
  opacity: 1;
}

.fade-enter-active,
.fade-leave-active {
  transition: all 0.2s;
}
.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
